package proyecto.testcasetool.presentation.details.usecase;

import org.eclipse.emf.common.notify.AdapterFactory;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.edit.domain.EditingDomain;
import org.eclipse.emf.edit.provider.AdapterFactoryItemDelegator;
import org.eclipse.emf.edit.provider.IItemPropertyDescriptor;
import org.eclipse.emf.edit.provider.ItemProvider;
import org.eclipse.emf.edit.ui.provider.AdapterFactoryContentProvider;
import org.eclipse.emf.edit.ui.provider.AdapterFactoryLabelProvider;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.ListViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.FocusAdapter;
import org.eclipse.swt.events.FocusEvent;
import org.eclipse.swt.events.KeyAdapter;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.List;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.forms.IFormPart;
import org.eclipse.ui.forms.widgets.ExpandableComposite;
import org.eclipse.ui.forms.widgets.FormToolkit;
import org.eclipse.ui.forms.widgets.Section;

import proyecto.testcasetool.Subject;
import proyecto.testcasetool.TestcasetoolPackage;
import proyecto.testcasetool.presentation.details.DetailsPage;
import proyecto.testcasetool.presentation.editor.pages.FormLayoutFactory;
import proyecto.testcasetool.provider.TestcasetoolEditPlugin;



public class SubjectDetailsPage extends DetailsPage {
	private Text txtSubjectName;
	private Text txtDescription;
	
	private AdapterFactoryItemDelegator afid;
	private ListViewer listViewerSubjectUseCases;
	
	public SubjectDetailsPage(FormToolkit toolkit, EditingDomain editingDomain,
			AdapterFactory adapterFactory) {
		super(toolkit, editingDomain, adapterFactory);
		afid =  new AdapterFactoryItemDelegator(adapterFactory);
	}

	@Override
	protected void createDetailsSection(final Composite parent) {
		GridLayout gridLayout = (GridLayout) parent.getLayout();
		gridLayout.verticalSpacing = 5;

		Section s = toolkit.createSection(parent, Section.TITLE_BAR);
		s.setText("System details");
		s.setLayoutData(new GridData(GridData.FILL_BOTH));
		s.setLayout(FormLayoutFactory.createDetailsGridLayout(false, 1));
		
		Composite client = toolkit.createComposite(s,SWT.FILL);
		
		s.setClient(client);
		client.setLayout(new GridLayout(2, false));
		toolkit.paintBordersFor(client);
		
		Label lblName = toolkit.createLabel(client, "Name", SWT.NONE);
		GridData gd_lblName = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1);
		gd_lblName.verticalIndent = 2;
		lblName.setLayoutData(gd_lblName);
		
		txtSubjectName = toolkit.createText(client, "New Text", SWT.NONE);
		
//		txtSubjectName.addModifyListener(new ModifyListener() {
//			public void modifyText(ModifyEvent e) {
//				IItemPropertyDescriptor desc = afid.getPropertyDescriptor(eObject,
//						TestcasetoolPackage.Literals.AGENT__NAME);
//				desc.setPropertyValue(eObject, ((Text) e.widget).getText());
//			}
//		});
		
		txtSubjectName.addFocusListener(new FocusAdapter() {
			
			private String previousValue = null;
			private String newValue = null;
			@Override
			@SuppressWarnings("synthetic-access")
			public void focusLost(FocusEvent e) {
				newValue = ((Text) e.widget).getText();
				
				if (previousValue == null || !previousValue.equals(newValue)){
					IItemPropertyDescriptor desc = afid.getPropertyDescriptor(
							eObject, TestcasetoolPackage.Literals.AGENT__NAME);
					desc.setPropertyValue(eObject, newValue);
					previousValue = newValue;
				}
				
				// TODO ver si hay que deshabilitar las opciones globales
//				if (propertiesEditionComponent != null) {
//					propertiesEditionComponent.firePropertiesChanged(new PropertiesEditionEvent(
//							UseCasePropertiesEditionPartForm.this,
//							EssentialUseCaseViewsRepository.UseCase.Properties.description,
//							PropertiesEditionEvent.COMMIT, PropertiesEditionEvent.SET, null, description.getText()));
//					propertiesEditionComponent
//							.firePropertiesChanged(new PropertiesEditionEvent(
//									UseCasePropertiesEditionPartForm.this,
//									EssentialUseCaseViewsRepository.UseCase.Properties.description,
//									PropertiesEditionEvent.FOCUS_CHANGED, PropertiesEditionEvent.FOCUS_LOST,
//									null, description.getText()));
//				}
			}

			/**
			 * @see org.eclipse.swt.events.FocusAdapter#focusGained(org.eclipse.swt.events.FocusEvent)
			 */
			@Override
			public void focusGained(FocusEvent e) {
				previousValue = ((Text) e.widget).getText();
				
//				if (propertiesEditionComponent != null) {
//					propertiesEditionComponent
//							.firePropertiesChanged(new PropertiesEditionEvent(
//									UseCasePropertiesEditionPartForm.this,
//									null,
//									PropertiesEditionEvent.FOCUS_CHANGED, PropertiesEditionEvent.FOCUS_GAINED,
//									null, null));
//				}
			}
		});
		
		txtSubjectName.addKeyListener(new KeyAdapter() {
			
			private String previousValue = null;
			private String newValue = null;
			/**
			 * @see org.eclipse.swt.events.KeyAdapter#keyPressed(org.eclipse.swt.events.KeyEvent)
			 * 
			 */
			@Override
			@SuppressWarnings("synthetic-access")
			public void keyPressed(KeyEvent e) {
				if (e.character == SWT.CR) {
					newValue = ((Text) e.widget).getText();
					if (previousValue == null
							|| !previousValue.equals(newValue)) {
						IItemPropertyDescriptor desc = afid
								.getPropertyDescriptor(
										eObject,
										TestcasetoolPackage.Literals.AGENT__NAME);
						desc.setPropertyValue(eObject, newValue);
						previousValue = newValue;
						txtSubjectName.forceFocus();
					}
					
				}
			}
		});
		
		GridData gd_txtUsecasename = new GridData(SWT.LEFT, SWT.CENTER, true, false, 1, 1);
		gd_txtUsecasename.verticalIndent = 2;
		gd_txtUsecasename.widthHint = 244;
		txtSubjectName.setLayoutData(gd_txtUsecasename);
		
		Label lblDescription = toolkit.createLabel(client, "Description", SWT.NONE);
		txtDescription = toolkit.createText(client, "", SWT.WRAP | SWT.V_SCROLL | SWT.MULTI);
		
		txtDescription.addFocusListener(new FocusAdapter() {
			@Override
			public void focusLost(FocusEvent e) {
				IItemPropertyDescriptor desc = afid.getPropertyDescriptor(
						eObject,
						TestcasetoolPackage.Literals.AGENT__DESCRIPTION);
				desc.setPropertyValue(eObject,
						((Text) e.widget).getText());
			}
		});
		
		GridData gd_txtDescription = new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1);
		gd_txtDescription.heightHint = 80;
		gd_txtDescription.verticalIndent = 10;
		gd_txtDescription.widthHint = 0;
		txtDescription.setLayoutData(gd_txtDescription);
		
		
		Composite sectionSystemUseCases = toolkit.createComposite(parent,SWT.FILL);
		sectionSystemUseCases.setLayout(new GridLayout(2, true));
		sectionSystemUseCases.setLayoutData(new GridData(GridData.FILL_BOTH));
		
		createSystemUseCaseSection(sectionSystemUseCases);
		
		
		
	}
	
	public void selectionChanged(IFormPart part, ISelection selection) {
		// controlar que sea la part que esto esperando!!!!
		if (!(selection instanceof IStructuredSelection)) {
			return;
		}
		EObject newEObject = getEObjectFromSelection(selection);
		if (newEObject != null && newEObject != eObject) {
			eObject = newEObject;
			if (eObject != null) {
				fillContents();
			}
		}
		else{
			refreshSystemUseCases();
		}
	}

	private void fillContents() {
		Subject system = (Subject)eObject;
		if (system.getName() != null){
			txtSubjectName.setText(system.getName());
		}
		if (system.getDescription() != null){
			txtDescription.setText(system.getDescription());
		}

		refreshSystemUseCases();
	}
	
	private void refreshSystemUseCases(){
		ItemProvider listRoot = new ItemProvider("UseCases",
				TestcasetoolEditPlugin.INSTANCE.getImage("full/obj16/Actor"),
				((Subject)eObject).getUseCase());
		listViewerSubjectUseCases.setInput(listRoot);
	}

	
	private void createSystemUseCaseSection(final Composite parent) {
		Section sectionUseCases = toolkit.createSection(parent,
				ExpandableComposite.EXPANDED | ExpandableComposite.TITLE_BAR);
		sectionUseCases.setText("System use cases");
		sectionUseCases.setLayoutData(new GridData(GridData.FILL_BOTH));
		sectionUseCases.setLayout(FormLayoutFactory.createDetailsGridLayout(false, 1));
		
		Composite compositeUseCases = toolkit.createComposite(sectionUseCases, SWT.NONE);
		sectionUseCases.setClient(compositeUseCases);
		compositeUseCases.setLayout(new GridLayout(2, false));
				
		
		Label lblUsesCases = toolkit.createLabel(compositeUseCases, "Use cases", SWT.NONE);
		listViewerSubjectUseCases = new ListViewer(compositeUseCases, SWT.BORDER | SWT.V_SCROLL);
		List list = listViewerSubjectUseCases.getList();
		GridData gd_list = new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1);
		gd_list.heightHint = 100;
		list.setLayoutData(gd_list);
		listViewerSubjectUseCases.setContentProvider(new AdapterFactoryContentProvider(adapterFactory));
		listViewerSubjectUseCases.setLabelProvider(new AdapterFactoryLabelProvider(adapterFactory));
	}
	
}